package com.lh_travel.lohas.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.*;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.lh_travel.lohas.R;

public class ShareDialog {
    private Dialog mDialog;
    //取消分享
    private Button btn_cancle;
    private OnClickListener mOnCancleListener;
    //微信分享
    private LinearLayout ll_wechat;
    private OnClickListener mOnWeChatShareListener;
    //微信朋友圈分享
    private LinearLayout ll_wechatmoment;
    private OnClickListener mOnWeChatFriendShareListener;
    //QQ分享
    private LinearLayout ll_qq;
    private OnClickListener mOnQQShareListener;
    private Context mContext;
    private Display display;

    public ShareDialog(Context context){
        mContext=context;
        //获取屏幕对象
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        builder();
    }

    //设置微信分享
    public ShareDialog setOnWeChatShare(OnClickListener weChatShareListener){
        mOnWeChatShareListener=weChatShareListener;
        return this;
    }

    //设置分享朋友圈
    public ShareDialog setOnWeChatFriendShare(OnClickListener weChatFriendShareListener){
        mOnWeChatFriendShareListener=weChatFriendShareListener;
        return this;
    }

    //QQ分享
    public ShareDialog setOnQQShare(OnClickListener qqShareListener){
        mOnQQShareListener=qqShareListener;
        return this;
    }

    public ShareDialog setOnCancleListener(OnClickListener cancleListener){
        mOnCancleListener=cancleListener;
        return this;
    }

    public void show(){
        mDialog.show();
    }

    public void dismiss(){
        mDialog.dismiss();
    }

    /**
     * 创建BaseDialog实例
     * @return
     */
    public ShareDialog builder(){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.share_dialg_style, null);
        //设置弹出框横向铺满整个屏幕
        view.setMinimumWidth(display.getWidth());
        mDialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);
        //设置dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
        mDialog.setCanceledOnTouchOutside(true);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        //设置点击隐藏
        btn_cancle=(Button)view.findViewById(R.id.btn_cancle);
        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnCancleListener != null) {
                    mOnCancleListener.onClick(mDialog, Dialog.BUTTON_NEGATIVE);
                }
                dismiss();
            }
        });

        //微信分享
        ll_wechat=(LinearLayout)view.findViewById(R.id.ll_wechat);
        ll_wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnWeChatShareListener != null) {
                    mOnWeChatShareListener.onClick(mDialog, Dialog.BUTTON_POSITIVE);
                }
                dismiss();
            }
        });

        //朋友圈分享
        ll_wechatmoment=(LinearLayout)view.findViewById(R.id.ll_wechatmoment);
        ll_wechatmoment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnWeChatFriendShareListener!=null) {
                    mOnWeChatFriendShareListener.onClick(mDialog, Dialog.BUTTON_POSITIVE);
                }
                dismiss();
            }
        });
        //QQ分享
        ll_qq=(LinearLayout)view.findViewById(R.id.ll_qq);
        ll_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnQQShareListener!=null){
                    mOnQQShareListener.onClick(mDialog,Dialog.BUTTON_POSITIVE);
                }
            }
        });
        mDialog.setContentView(view);
        return this;
    }
}
